"""
test - 测试

Author: zhaopin
Date: 2024/4/10
"""
import csv
import re
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

import PyPDF2 as PyPDF2
import openpyxl
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas


def test_email1():
    """email操作-"""
    email = MIMEMultipart()
    email['From'] = 'jack@126.com'
    email['To'] = '2837@qq.com'
    email['Subject'] = '...'

    message = MIMEText('...', 'plain', 'utf-8')
    email.attach(message)
    attachment = MIMEText(b'...', 'base64', 'utf-8')
    attachment['content-type'] = 'application/octet-stream'
    attachment['content-disposition'] = 'attachment; filename="..."'
    email.attach(attachment)

    smtp = smtplib.SMTP_SSL(host='stmp.126.com', port=465)
    smtp.login('jack@126.com', '...')
    smtp.sendmail('jack@126.com', ['2837@qq.com'], email.as_string())


def test_xlsx1():
    """xlsx操作-"""
    workbook = openpyxl.load_workbook('resources/aa.xlsx')
    worksheet = workbook.worksheets[0]
    data = [[cell.value for cell in row]
            for row in worksheet[f'A3:G{worksheet.max_row}']]
    with open('resources/sales_data.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerow(('aa', 'bb', 'cc', 'dd', 'ff'))
        writer.writerows(data)


def test_pdf1():
    """pdf操作-旋转"""
    reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf')
    writer = PyPDF2.PdfFileWriter()
    for page_num in range(reader.numPages):
        current_page = reader.getPage(page_num)
        print(current_page.extractText())
        current_page.rotateClockwise(90)
        writer.addPage(current_page)
        writer.addBlankPage()

    with open('resources/XGBoost-modified.pdf', 'wb') as file:
        writer.write(file)


def test_pdf2():
    """pdf操作-加密"""
    reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf')
    writer = PyPDF2.PdfFileWriter()
    for page_num in range(reader.numPages):
        writer.addPage(reader.getPage(page_num))
    writer.encrypt('foobared')

    with open('resources/XGBoost-modified.pdf', 'wb') as file:
        writer.write(file)


def test_pdf3():
    """pdf操作-解密"""
    reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf')
    if reader.decrypt('foobared') == 1:
        print('密码错误')


def test_pdf4():
    """pdf操作-加水印"""
    reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf')
    watermark = PyPDF2.PdfFileReader('resources/watermark.pdf')
    writer = PyPDF2.PdfFileWriter()
    watermark_page = watermark.getPage(0)
    for page_num in range(reader.numPages):
        current_page = reader.getPage(page_num)
        current_page.mergePage(watermark_page)
        writer.addPage(current_page)
    with open('resources/XGBoost-modified.pdf', 'wb') as file:
        writer.write(file)


def test_pdf5():
    """pdf操作-创建"""
    pdf_canvas = canvas.Canvas('resources/demo.pdf', pagesize=A4)
    width, height = A4

    # 绘图
    image = canvas.ImageReader('resources/guido.jpg')
    pdf_canvas.drawImage(image, 20, height - 395, 250, 375)

    # 显示当前页
    pdf_canvas.showPage()

    # 注册字体
    pdfmetrics.registerFont(TTFont('Font1', 'resources/fonts/Vera.ttf'))
    pdfmetrics.registerFont(TTFont('Font2', 'resources/fonts/青呱石头体.ttf'))

    # 写字
    pdf_canvas.setFont('Font2', 40)
    pdf_canvas.setFillColorRGB(0.9, 0.5, 0.3, 1)
    pdf_canvas.drawString(width // 2 - 120, height // 2, '你好，世界！')
    pdf_canvas.setFont('Font1', 40)
    pdf_canvas.setFillColorRGB(0, 1, 0, 0.5)
    pdf_canvas.rotate(18)
    pdf_canvas.drawString(250, 250, 'Hello world!')

    # 保存
    pdf_canvas.save()


def test_re():
    """测试正则匹配"""
    username = 'adf$$$4sdfa#'
    matched = re.match('\\w{6,20}', username)
    # r表示原始字符串，里面不包含转义
    matched = re.match(r'\w{6,20}', username)
    # 全匹配
    username = 'sdfafsfsdfal5323dsfdasdlfdaj'
    matched = re.match(r'^\w{6,20}$', username)
    matched = re.fullmatch(r'\w{6,20}', username)


if __name__ == '__main__':
    test_re()
